package com.smit.common;

import android.content.Context;
import android.os.Build;

import com.smit.utils.LogUtil;

import org.json.JSONObject;

/**
 * 获取鉴权码
 */
public final class AuthControl {
    /**
     * 鉴权失败
     */
    public static final int AUTH_FAIL = -1;
    /**
     * 鉴权状态未知
     */
    public static final int AUTH_NONE = 0;
    /**
     * 鉴权成功
     */
    public static final int AUTH_OK = 1;

    private static final String TAG = "AuthControl";
    private AuthThread authThread;

    private int authStatus = AUTH_NONE;
    private String apik;

    private Context context;
    private String dongleId;
    private String dongleType;
    private String ethMacAddr;
    private String wifiMacAddr;
    //private String hostBrand;
    //private String hostType;
    //private String hostAndroidVersion;
    //private String icastSdkVersion;
    private String appId;
    private String appKey;
    private JSONObject authResult;
    /**
     * 获取DVBAdapter实例
     *
     * @return DVBAdapter单例
     */
    public static AuthControl getInstance() {
        return SingletonHolder.instance;
    }

    /**
     * 初始化 AuthControl
     *
     * @param context Android Application Context
     */
    public void initialize(Context context) {
        this.context = context;
    }

    /**
     * 设置鉴权状态
     *
     * @param status 鉴权状态
     * @see #AUTH_FAIL
     * @see #AUTH_NONE
     * @see #AUTH_OK
     */
    public void setAuthStatus(int status) {
        authStatus = status;
    }

    /**
     * 返回鉴权状态
     *
     * @return 鉴权状态
     * @see #AUTH_FAIL
     * @see #AUTH_NONE
     * @see #AUTH_OK
     */
    public int getAuthStatus() {
        return authStatus;
    }

    /**
     * 设置apik
     *
     * @param apikStr 鉴权码
     */
    public void setApik(String apikStr) {
        apik = apikStr;
    }

    /**
     * 开始鉴权
     */
    public void startAuth() {
        if (authThread == null) {
            authThread = new AuthThread(context);
            authThread.start();
        }
    }

    /**
     * 鉴权开始状态是wait的，等待设备状态正常后，调用这个函数取消wait状态
     * @param wait 是否进入等待状态
     */
    public void setAuthWaitFlag(boolean wait) {
        if (authThread != null) {
            authThread.setWaitFlag(wait);
        }
    }

    /**
     * 停止鉴权
     */
    public void stopAuth() {
        if (authThread != null) {
            authThread.interrupt();
            authThread = null;
        }
    }

    /**
     * 获取apik
     *
     * @return 直接返回，如果没有就返回null
     */
    public String getApik() {
        if (authStatus == AUTH_OK) {
            return apik;
        } else {
            LogUtil.error(TAG, "getApik authStatus is not AUTH_OK");
            return null;
        }
    }

    /**
     * 获取apik，等待超时返回
     *
     * @param timeout 超时时间,单位ms
     * @return 成功返回apik，失败返回null
     */
    public String getApikWait(int timeout) {
        int timeout2 = timeout;
        while (timeout2 > 0) {
            if (authStatus == AUTH_OK) {
                return apik;
            } else if (authStatus == AUTH_FAIL) {
                LogUtil.error(TAG, "getApikWait AUTH_FAIL");
                return null;
            } else {
                try {
                    LogUtil.trace(TAG, "getApikWait sleep 100ms");
                    Thread.sleep(100);
                    timeout2 -= 100;
                } catch (InterruptedException e) {
                    LogUtil.error(TAG, "getApikWait InterruptedException");
                }

            }
        }
        return null;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }

    public String getAppKey() {
        return appKey;
    }

    public void setDongleId(String dongleId) {
        this.dongleId = dongleId;
    }

    public String getDongleId() {
        return dongleId;
    }

    public void setDongleType(String dongleType) {
        this.dongleType = dongleType;
    }

    public String getDongleType() {
        return dongleType;
    }

    public void setEthMacAddr(String ethMacAddr) {
        this.ethMacAddr = ethMacAddr;
    }

    public String getEthMacAddr() {
        return ethMacAddr;
    }

    public void setWifiMacAddr(String wifiMacAddr) {
        this.wifiMacAddr = wifiMacAddr;
    }

    public String getWifiMacAddr() {
        return wifiMacAddr;
    }

    public String getHostBrand() {
        return Build.BRAND;
    }

    public String getHostAndroidVersion() {
        return String.valueOf(android.os.Build.VERSION.SDK_INT);
    }

    public String getHostType() {
        return Build.TYPE;
    }

    public String getHostProductName() {
        return Build.PRODUCT;
    }

    public JSONObject getAuthResult() {
        return authResult;
    }

    public String getIcastSdkVersion() {
        return String.valueOf(AppData.SDK_API_LEVEL);
    }

    protected void setAuthResult(JSONObject result) {
        authResult = result;
    }

    private static class SingletonHolder {
        private static AuthControl instance = new AuthControl();
    }
}
